
//  HotViewController.m
//  HomePage
//
//  Created by dlios on 15-1-28.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TingMusicViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "ScrollViewNSTimer.h"
#import "List.h"
#import "PhotoAlbum.h"
#import "HotCell.h"
#import "PhotoAlbumViewController.h"
#import "HotSecondViewController.h"
@interface TingMusicViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)NSMutableArray *arrPhoto;
@property (nonatomic, retain)UIScrollView *scrollViewLittle;
@property (nonatomic, retain)UIPageControl *pageControl;
@property (nonatomic, assign)NSInteger numb;
@property (nonatomic, retain)UICollectionView *collectView;
@property (nonatomic, assign)NSInteger a;
@property (nonatomic, retain)NSDictionary *dic;
@property (nonatomic,retain)UIView *viewback;
@property (nonatomic,retain)ScrollViewNSTimer *timer;

//@property (nonatomic, retain)UIImageView *imageviewPhoto;
@end

@implementation TingMusicViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.a = 0 ;
    self.view.backgroundColor = [UIColor colorWithRed:225/255.0 green:237/255.0 blue:238/255.0 alpha:1];
    [self CreateCollectionView];
    [self CreateScrollView];
    [self showDictionary];
    
    
}

- (void)showDictionary
{
    
    //    NSString *str = @"http://mobile.ximalaya.com/m/category_tag_list?device=android&per_page=100&category=all&type=album&page=1";
    NSString *str = @"http://mobile.ximalaya.com/m/category_tag_list?device=android&per_page=100&category=lvyou&type=album&page=1";
    NSLog(@"=====================%@",str);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObject:@"application/json"];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.dic = responseObject;
        NSMutableArray *arry = [self.dic objectForKey:@"list"];
        self.arr = [NSMutableArray array];
        for (NSDictionary *dicList in arry) {
            List *lis = [[List alloc] init];
            [lis setValuesForKeysWithDictionary:dicList];
            [self.arr addObject:lis];
            
        }
        [self.collectView reloadData];
       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //  NSLog(@"error: %@",error);
    }];
    
}
- (void)CreateScrollView
{
    
   
    NSString *str = @"http://mobile.ximalaya.com/m/category_focus_image?categoryId=22&device=android";
    
    NSLog(@"+++++++++++%@", str);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSMutableArray *arry = [dic objectForKey:@"list"];
        self.arrPhoto = [NSMutableArray array];
        
        for (NSDictionary *dicPhoto in arry) {
            PhotoAlbum *photo = [[PhotoAlbum alloc] init];
            [photo setValuesForKeysWithDictionary:dicPhoto];
            [self.arrPhoto addObject:photo];
        }
        // NSLog(@"********************%@",self.arrPhoto);
        
        NSMutableArray *arrr = [NSMutableArray array];
        
        self.timer = [[ScrollViewNSTimer alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height / 3.787 )];
        self.timer.timerTarget = self;
        //self.timer.backgroundColor = [UIColor redColor];
        
        //        [self.timer.button addTarget:self action:@selector(buttonCliked:) forControlEvents:UIControlEventTouchUpInside];
        
        for (NSInteger i = 0; i < self.arrPhoto.count; i++) {
            NSString *string = [[self.arrPhoto objectAtIndex:i]pic];
            [arrr addObject:string];
            
        }
        self.view.userInteractionEnabled = YES;
        [self.timer creatScroviewNstimer:arrr];
        [self.view addSubview:self.timer];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //  NSLog(@"error: %@",error);
    }];
}
- (void)buttonCliked:(UIButton *)button
{
    int tag = button.tag - 100;
    
    PhotoAlbumViewController *photoVC = [[PhotoAlbumViewController alloc] init];
    PhotoAlbum *Album = [self.arrPhoto objectAtIndex:tag];
    //  NSLog(@"%d",button.tag);
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    NSString *str = [formatter stringFromNumber:Album.type];
    int intStr = [str intValue];
    if (intStr == 2) {
        PhotoAlbum *photo = [self.arrPhoto objectAtIndex:tag];
        photoVC.numberTwo = photo.albumId;
        photoVC.jibie = Album.type;
        [self.navigationController pushViewController:photoVC animated:YES];
    }
    
    
    
    
}

- (void)CreateCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width / 3.5, self.view.frame.size.height / 5.68);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(74, 10, 20, 10);
    self.collectView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height / 3.787 , self.view.frame.size.width , self.view.frame.size.height - self.view.bounds.size.height / 3.787 - 44) collectionViewLayout:flowLayout];
    _collectView.backgroundColor = [UIColor colorWithRed:225/255.0 green:237/255.0 blue:238/255.0 alpha:1];
    _collectView.delegate = self;
    _collectView.dataSource = self;
    [self setAutomaticallyAdjustsScrollViewInsets:NO ];
    // [self.collectView setAutoresizesSubviews:YES];
    [self.view addSubview:_collectView];
    [_collectView registerClass:[HotCell class] forCellWithReuseIdentifier:@"hot"];
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotSecondViewController *hotSecond = [[HotSecondViewController alloc] init];
    hotSecond.leibie = @"lvyou";
    hotSecond.tname = [[self.arr objectAtIndex:indexPath.item] tname];
    [self.navigationController pushViewController:hotSecond animated:YES];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"hot" forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    List *lis = [self.arr objectAtIndex:indexPath.item];
    cell.lis = lis;
    cell.labelTname.text = lis.tname;
    [cell.imageViewCover_path sd_setImageWithURL:[NSURL URLWithString:lis.cover_path]  placeholderImage:[UIImage imageNamed:@"placeholder.jpg"]];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
